<?php
namespace app\admin\controller;
use app\admin\model\Beaut_home;
use app\index\controller\PaymentController;
use think\Db;
/**
 * 美丽家管理
 * Class Member
 */
class BeauthomeController extends BaseController {

    protected function _initialize() {
        parent::_initialize();
    }

    /**
     * 美丽家管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '', $ziduan = 'id', $param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map[$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
        $ad_list = db('beaut_home')->where($map)->order($order)->paginate(15, false, get_query());
        return $this->fetch('index', ['home_list' => $ad_list, 'keyword' => $keyword]);
    }
	
    /**
     * 添加美丽家
     * @return mixed
     */
    public function add() {
        return $this->fetch();
    }

    /**
     * 保存美丽家
     */
    public function save() {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
			$data['add_time'] = now_time();
			$home_model = new Beaut_home();
            if ($home_model->allowField(true)->save($data)) {
                $this->success('添加成功',url("index"));
            } else {
                $this->error('添加失败');
            }
        }
    }

    /**
     * 编辑美丽家
     * @param $id
     * @return mixed
     */
    public function edit($id) {
    	$home_model = new Beaut_home();
        $home = $home_model->where('id',$id)->find();		
        return $this->fetch('edit', ['home' => $home]);
    }

    /**
     * 更新美丽家
     * @param $id
     */
    public function update($id) {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
			$home_model = new Beaut_home();
            if ($home_model->allowField(true)->save($data,['id'=>$id]) !== false) {
                $this->success('更新成功',url("index"));
            } else {
                $this->error('更新失败');
            }
        }
    }
	
	//删除美丽家
	public function delete_item(){
		$data['state'] = 1;
		$res = db('beaut_home')->where('id',input("id"))->update($data);
		if($res){
			 $this->success('删除成功',url("index"));
		}else{
			 $this->success('删除失败');
		}
	}
	
	/**
	 * 状态设置
	 * @return mixed
	 */
	public function status() {
		$ids = input('param.id/a');

		$val = input('param.val');
		$res = db('beaut_home')->where('id','in', $ids)->setField('is_on_sale', $val);
		if ($res === false) {
			$this->error('设置失败');
		}
		$this->success('设置成功');
	}

	//预定信息管理	
	public function reservelist(){
		$list = db("reserve")->where('home_id',input('id'))->paginate(15, false, get_query());
		$this->assign('list',$list);
		return $this->fetch();
	}
	
	 /**
     * 美丽家管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function orders($keyword = '', $ziduan = 'id', $param='add_time', $sort='desc') {
        $map = [];
		$order = "a.".$param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map["a.".$ziduan] = $keyword;
        }
		if (input('state')!='') {
            $map['a.state'] = input('state');
        }
		$this->assign('ziduan', $ziduan);
        $ad_list = db('reserve')->alias("a")
		->join("beaut_home b","a.home_id=b.id","LEFT")
		->field("a.*,b.home_sn,b.home_name")
        ->where($map)->order($order)->paginate(15, false, get_query());
        return $this->fetch('orders', ['home_list' => $ad_list, 'keyword' => $keyword]);
    }
	
	//关闭
	public function delete_gb() {
   	 	$parm['state'] = 3;
   	 	$result = db('reserve')->where('id',input('id'))->update($parm);
        if($result){
			return json(['code'=>200,'msg'=>'操作成功']);
        }else{
       		return json(['code'=>402,'msg'=>'操作失败']);
        }
	}
	
	    /**
     * 操作预定信息
     * @param $id
     * @return mixed
     */
    public function ordersedit($id) {
		$home = db('reserve')->alias("a")
		->join("beaut_home b","a.home_id=b.id","LEFT")
		->field("a.*,b.home_sn,b.home_name")->where('a.id',$id)->find();	
        return $this->fetch('ordersedit', ['home' => $home]);
    }

    /**
     * 更新预定信息
     * @param $id
     */
    public function ordersupdate($id) {
        if ($this->request->isPost()) {
            $data  = $this->request->post();
			$parm['state'] = $data['state'];
   	 		$result = db('reserve')->where('id',$data['id'])->update($parm);
            if ($result) {
            	if($data['state']==2){
            		Db::startTrans();	
					$result = true;
					$reserve_info = db('reserve')->where('id',$data['id'])->find();
					$beaut_home = db('beaut_home')->where('id',$reserve_info['home_id'])->find();
					$pay_user_info = db('member')->where('id',$reserve_info['user_id'])->find();
            		//获取第一上级用户信息
					$user_pid1 = db('member')->where('id',$pay_user_info['pid1'])->find();
					$payment=new PaymentController();
					if($user_pid1){
						//计算第一上级累计佣金
						$user_pid1_total = bcadd($user_pid1['total_yj'], $beaut_home['hy1_balance'], 2);
						//计算待提现佣金
						$rest4 = gdyj_record_log($user_pid1['id'],$beaut_home['hy1_balance'],"直推会员预定产品奖励",10,$reserve_info['order_sn'],$pay_user_info['pt_id'],$pay_user_info['city_id'],$pay_user_info['id']);
						$payment->send_pay($user_pid1['open_id'],'',$beaut_home['hy1_balance'],'直推会员预定产品奖励');
						if(!$rest4) $result =false;
					}
		
					//获取第二上级用户信息
					$user_pid2 = db('member')->where('id',$pay_user_info['pid2'])->find();
					if($user_pid2){
						//计算第一上级累计佣金
						$user_pid2_total = bcadd($user_pid2['total_yj'], $beaut_home['hy2_balance'], 2);
						$rest6 = gdyj_record_log($user_pid2['id'],$beaut_home['hy2_balance'],"间推会员预定产品奖励",11,$reserve_info['order_sn'],$pay_user_info['pt_id'],$pay_user_info['city_id'],$pay_user_info['id']);
						$payment->send_pay($user_pid2['open_id'],'',$beaut_home['hy2_balance'],'间推会员预定产品奖励');
						if(!$rest6) $result =false;
					}
		
					//获取合伙人信息
					$user_pt = db('member')->where('id',$pay_user_info['pt_id'])->find();
					if($user_pt){
						//计算第一上级累计佣金
						$user_pt_total = bcadd($user_pt['total_yj'], $beaut_home['pt_balance'], 2);
						//计算待提现佣金
						$user_pt_dtx_yj = bcadd($user_pt['yj'], $beaut_home['pt_balance'], 2);
						//更新第一级用户信息
						$rest5 = db('member')->where('id', $user_pt['id'])
										->update(['total_yj'=>$user_pt_total,'yj'=>$user_pt_dtx_yj,'update_time'=>now_time()]);
						if(!$rest5) $result =false;
						//佣金变化日志
						$rest6 = gdyj_record_log($user_pt['id'],$beaut_home['pt_balance'],"会员预定产品合伙人奖励",12,$reserve_info['order_sn'],$pay_user_info['pt_id'],$pay_user_info['city_id'],$pay_user_info['id']);
						if(!$rest6) $result =false;
					}
					//获取运营中心信息
					$user_city = db('member')->where('id',$pay_user_info['city_id'])->find();
					if($user_city){
						//计算第一上级累计佣金
						$user_city_total = bcadd($user_city['total_yj'], $beaut_home['city_balance'], 2);
						//计算待提现佣金
						$user_city_dtx_yj = bcadd($user_city['yj'], $beaut_home['city_balance'], 2);
						//更新第一级用户信息
						$rest7 = db('member')->where('id', $user_city['id'])
										->update(['total_yj'=>$user_city_total,'yj'=>$user_city_dtx_yj,'update_time'=>now_time()]);
						if(!$rest7) $result =false;
						//佣金变化日志
						$rest8 = gdyj_record_log($user_city['id'],$beaut_home['city_balance'],"会员预定产品运营中心奖励",13,$reserve_info['order_sn'],0,$pay_user_info['city_id'],$pay_user_info['id']);
						if(!$rest8) $result =false;
					}
					if($result){
						Db::commit();
						 $this->success('操作成功',url("orders"));
					}else{
						Db::rollback();
						  $this->error('操作失败');
					}
            	}
                $this->success('操作成功',url("orders"));
            } else {
                $this->error('操作失败');
            }
        }
    }
}
